
#include <curses.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

#include "read_fan_temperature.h"
#include "../ec_it8733_inc/ec_it8733_dev.h"

#include "ec_it8733_rw.h"

//#define DBG_UART_PRT
//#include "dbg_prt.h"
#include "../ec_it8733_inc/dbg_prt.h"

#if 0
uos-20@uos-20-PC:~/gitsvr/gitee/little_demo/ncurses_study/ttyrw$ sudo dmidecode -s bios-vendor
Kunlun
uos-20@uos-20-PC:~/gitsvr/gitee/little_demo/ncurses_study/ttyrw$ sudo dmidecode -s bios-version
Kunlun-V4.0.12V4.0 LS3A4000
uos-20@uos-20-PC:~/gitsvr/gitee/little_demo/ncurses_study/ttyrw$ sudo dmidecode -s bios-release-date
12/27/2019
#endif

static int get_bios_ver(const char* strcmd, char* strVer, int nlenVer)
{
    FILE *fstream = NULL;
    char buffer[1024];
    int found = -1;
 
    memset(buffer, 0, sizeof(buffer));  
 
    //只ping一次 
    //sprintf(buffer, "ping -c 1 %s", ip);
    sprintf(buffer, "%s", strcmd);
    if (NULL == (fstream = popen(buffer,"r")))      
    {    
	//printf("NULL == popen\n");
        return -1;      
    }   
 
    //读取执行结果
    while (NULL != fgets(buffer, sizeof(buffer), fstream)) 
    {  
        //LOG_INFO("%s", buffer);
        //包含特定字串，表示执行成功
        if (strlen(buffer) > 0)
        {
            //找到了

            strncpy(strVer,buffer,nlenVer);
			//printf("%s\n",buffer);
            found = 0;
            break;
        }
    }
 
    pclose(fstream);    
 
    return found;     
}


static void init_curses()
{
        initscr();
        start_color();
        init_pair(1,COLOR_WHITE,COLOR_BLUE);
        init_pair(2,COLOR_BLUE,COLOR_WHITE);
        init_pair(3,COLOR_RED,COLOR_WHITE);
        curs_set(0);
        noecho();
        keypad(stdscr,TRUE);
}
static void draw_menubar(WINDOW *menubar)
{
        int i=0;
        int nMenuWidth=20;

        //wbkgd(menubar,COLOR_PAIR(2));

        wmove(menubar,0,nMenuWidth*i);
        waddstr(menubar,"test ttyS1");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(F4)");
        //wattroff(menubar,COLOR_PAIR(3));

        i++;
        wmove(menubar,0,nMenuWidth*i);
        waddstr(menubar,"test clear");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(F5)");
        //wattroff(menubar,COLOR_PAIR(3));


        i++;
        wmove(menubar,0,nMenuWidth*i);
        waddstr(menubar,"EXIT");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(F9)");
        //wattroff(menubar,COLOR_PAIR(3));

}

extern EN_RUN_STATE_TYPE en_run_state;
extern EN_KEY_VALUE_TYPE en_key_value;
void *thr_fn(void *arg);

static WINDOW *msgBoxVendor;
static WINDOW *msgBoxVersion;
static WINDOW *msgBoxReleaseDate;

static WINDOW *messagebar;

static void disp_ttyrw_send(const char* sval)
{
        mvwprintw(msgBoxVendor,1,20,"%s",sval);
        touchwin(stdscr);
        refresh();
}

static void disp_ttyrw_recv(const char* sval)
{
        mvwprintw(msgBoxVersion,1,20,"%s",sval);
        touchwin(stdscr);
        refresh();
}

static void disp_ttyrw_status(const char* sval)
{
        mvwprintw(msgBoxReleaseDate,1,20,"%s",sval);
        touchwin(stdscr);
        refresh();
}


static void disp_sleep_msg(char* str)
{
        mvwprintw(messagebar,4,2,"%s",str);
        touchwin(stdscr);
        refresh();
}

static void create_disp_box()
{
    int nboxheight=3;
    int nboxwidth=80;
    
    int nboxstarty=4;
    int nboxstartx=1;
    int nboxgap=3;

    msgBoxVendor=subwin(stdscr,nboxheight,nboxwidth,nboxstarty,nboxstartx);
    box(msgBoxVendor,ACS_VLINE,ACS_HLINE);
    mvwprintw(msgBoxVendor,1,2,"vendor: ");


    nboxstarty+=nboxgap;
    msgBoxVersion=subwin(stdscr,nboxheight,nboxwidth,nboxstarty,nboxstartx);
    box(msgBoxVersion,ACS_VLINE,ACS_HLINE);
    mvwprintw(msgBoxVersion,1,2,"version: ");

    nboxstarty+=nboxgap;
    msgBoxReleaseDate=subwin(stdscr,nboxheight,nboxwidth,nboxstarty,nboxstartx);
    box(msgBoxReleaseDate,ACS_VLINE,ACS_HLINE);
    mvwprintw(msgBoxReleaseDate,1,2,"release date: ");


    nboxstarty+=nboxgap;
    messagebar=subwin(stdscr,10,nboxwidth,nboxstarty,1);
    //messagebar=subwin(stdscr,10,79,35,1);
    box(messagebar,ACS_VLINE,ACS_HLINE);
    mvwprintw(messagebar,2,2,"msg: ");

}


int win_stdscr_bios_ver()
{
    int ch;
    WINDOW *menubar;

    init_curses();

    //bkgd(COLOR_PAIR(1));

    menubar=subwin(stdscr,2,80,0,0);

    create_disp_box();

    draw_menubar(menubar);
    //move(2,1);
    //printw("Press F1 or F2 to open the menus. ");
    //printw("ESC quits.");
    refresh();

    do
    {

        ch=getch();

        //dbg_prt( "en_run_state: %4d; ch=%d", en_run_state, ch );
        dbg_prt( "en_run_state: %4d; ch=%d", en_run_state, ch );
        mvwprintw(messagebar,2,10,"en_run_state: %4d; ch=%d", en_run_state, ch);

        
        if(ch == KEY_F(4))
        {
                char strVer[64];
                int nlenVer=sizeof(strVer);

                //static int get_bios_ver(const char* strcmd, char* strVer, int nlenVer);
                get_bios_ver("dmidecode -s bios-vendor", strVer, nlenVer);
                strVer[strlen(strVer)-1]=0x00;
                disp_ttyrw_status(strVer);
                dbg_prt( "strVer =%s\n", strVer );

                get_bios_ver("dmidecode -s bios-version", strVer, nlenVer);
                strVer[strlen(strVer)-1]=0x00;
                disp_ttyrw_send(strVer);

                get_bios_ver("dmidecode -s bios-release-date", strVer, nlenVer);
                strVer[strlen(strVer)-1]=0x00;
                disp_ttyrw_recv(strVer);

        }

        if(ch == KEY_F(5))
        {
                char strVer[64];
                memset(strVer,0x00,sizeof(strVer));
                memset(strVer,' ',sizeof(strVer)/2);

                disp_ttyrw_send(strVer);
                disp_ttyrw_recv(strVer);
                disp_ttyrw_status(strVer);
        }

        else if (ch == KEY_F(9))
        {

                en_run_state=RUN_STATE_EXIT;
                dbg_prt( "en_run_state: %4d ", en_run_state );
                mvwprintw(messagebar,3,2,"en_run_state: %4d ", en_run_state);
        }
        
        
        touchwin(stdscr);
        //werase(messagebar);
        wrefresh(messagebar);

        refresh();


    } while (ch!=KEY_F(9));

    delwin(menubar);
    delwin(messagebar);
    endwin();
    return 0;
}


